package com.v0710.tempro;

import com.intellij.notification.*;
import com.intellij.openapi.ui.DialogWrapper;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.event.ActionEvent;

/**
 * @Author YIFan
 * @Date 2021-7-29 10:57
 * @Version 1.0
 */
public class Dialog extends DialogWrapper {

    private JTextField ip;
    private JTextField user;
    private JTextField pwd;
    private JTextField dbName;
    private JTextField module;
    private JTextField tableName;
    private JTextField deletedField;
    private JTextField pName;
    private JTextField pPath;

    private String proname;
    private String path;
    private String bname;

    private String pomStr= "<dependency>\n" +
            "\t<groupId>com.baomidou</groupId>\n" +
            "\t<artifactId>mybatis-plus-boot-starter</artifactId>\n" +
            "\t<version>3.4.2</version>\n" +
            "</dependency>\n" +
            "\n" +
            "<!-- mybatis-plus 代码生成器-->\n" +
            "<dependency>\n" +
            "\t<groupId>com.baomidou</groupId>\n" +
            "\t<artifactId>mybatis-plus-generator</artifactId>\n" +
            "\t<version>3.4.1</version>\n" +
            "</dependency>\n" +
            "\n" +
            "<dependency>\n" +
            "\t<groupId>org.apache.velocity</groupId>\n" +
            "\t<artifactId>velocity-engine-core</artifactId>\n" +
            "\t<version>2.0</version>\n" +
            "</dependency>";

    protected Dialog(String proname,String path ,String bname) {
        super(true);
        this.proname = proname;
        this.path = path;
        this.bname = bname;
        setTitle("模板代码生成-"+proname);
        init();
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        JPanel jPanel = new JPanel();
        jPanel.setMinimumSize(new Dimension(530,300));
        jPanel.setPreferredSize(new Dimension(630,400));
        jPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

        /* 这个方法定义了组件的位置。
         * setBounds(x, y, width, height)
         * x 和 y 指定左上角的新位置，由 width 和 height 指定新的大小。
         */

        // 创建 JLabel
        JLabel dbip = new JLabel("ip端口:");
        dbip.setPreferredSize(new Dimension(70,40));
        dbip.setForeground(Color.red);
        jPanel.add(dbip);
        ip = new JTextField(20);
        ip.setText("127.0.0.1:3306");
        jPanel.add(ip);
        ip.requestFocus();

        /*==========================================*/

        JLabel dbuser = new JLabel("数据库账户:");
        dbuser.setForeground(Color.red);
        dbuser.setPreferredSize(new Dimension(70,40));
        jPanel.add(dbuser);

        user = new JTextField(20);
        user.setText("root");
        jPanel.add(user);

        /*==========================================*/

        JLabel dbpwd = new JLabel("数据库密码:");
        dbpwd.setForeground(Color.red);
        dbpwd.setPreferredSize(new Dimension(70,40));
        jPanel.add(dbpwd);

        pwd = new JTextField(20);
        pwd.setText("root");
        jPanel.add(pwd);

        /*==========================================*/

        JLabel dbname = new JLabel("数据库名称:");
        dbname.setForeground(Color.red);
        dbname.setPreferredSize(new Dimension(70,40));
        jPanel.add(dbname);

        dbName = new JTextField(20);
        dbName.setText("possess");
        jPanel.add(dbName);

        /*==========================================*/

        JLabel dbtable = new JLabel("表名:");
        dbtable.setForeground(Color.red);
        dbtable.setPreferredSize(new Dimension(70,40));
        jPanel.add(dbtable);

        tableName = new JTextField(20);
        tableName.setText("");
        jPanel.add(tableName);

        /*==========================================*/

        JLabel dbbname = new JLabel("项目包名:");
        dbbname.setForeground(Color.red);
        dbbname.setPreferredSize(new Dimension(70,40));
        jPanel.add(dbbname);

        pName = new JTextField(20);
        pName.setText(bname);
//        pName.setText("com.v0710.demoplug");
        jPanel.add(pName);


        /*==========================================*/

        JLabel dbdelField = new JLabel("逻辑删除字段:");
        dbdelField.setPreferredSize(new Dimension(70,40));
        jPanel.add(dbdelField);

        deletedField = new JTextField(20);
        deletedField.setText("");
        jPanel.add(deletedField);

        /*==========================================*/

        JLabel dbpath = new JLabel("项目路径:");
        dbpath.setForeground(Color.red);
        dbpath.setPreferredSize(new Dimension(70,40));
        jPanel.add(dbpath);

        pPath = new JTextField(20);
        pPath.setText(path);
//        pPath.setText("E:/_Code/Java_Code/demoplug");
        jPanel.add(pPath);

        /*==========================================*/

        JLabel dbmodule = new JLabel("模块名称:");
        dbmodule.setPreferredSize(new Dimension(70,40));
        jPanel.add(dbmodule);

        module = new JTextField(20);
        module.setText("");
        jPanel.add(module);

        JButton jButton = new JButton("mp,mysql,依赖点击复制");
        jButton.setAction(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
                Transferable tText = new StringSelection(pomStr);
                clip.setContents(tText, null);
            }
        });
        jButton.setText("mp,mysql,依赖点击复制");
        jPanel.add(jButton);


        jPanel.setVisible(true);
        return jPanel;
    }

    /*取消*/
    @Override
    public void doCancelAction() {
        System.err.println("doCancelAction");
        super.doCancelAction();
    }

    /*确认*/
    @Override
    protected void doOKAction() {
        System.err.println("ok");
        String ipText = ip.getText();
        String userText = user.getText();
        String pwdText = pwd.getText();
        String dbnameText = dbName.getText();
        String moduledText = module.getText();
        String tableNamedText = tableName.getText();
        String deletedFieldText = deletedField.getText();

        AppInfo info = new AppInfo();
        info.setAuthor("v0710");
        info.setDbName(dbnameText);
        info.setDbPassword(pwdText);
        info.setDbPort(ipText);
        info.setDbUsername(userText);
        info.setDeletedField(deletedFieldText);
        info.setModuleName(moduledText);
        info.setPackageName(pName.getText());
        info.setPath(pPath.getText());
        info.setTableName(tableNamedText);

        String auto = GeneratebyMybatisplus.auto(info);

        NotificationGroup group = new NotificationGroup("项目模板插件", NotificationDisplayType.BALLOON,true);
        Notification msg = group.createNotification("创建完成","subtitle",auto, NotificationType.INFORMATION);
        Notifications.Bus.notify(msg);

        super.doOKAction();
    }

    public static void main(String[] args) {
        String projectPath = System.getProperty("user.dir");
        System.out.println("projectPath==" + projectPath);

    }

}
